//
//  PickHeaderCell.m
//  ProjectforHDX
//
//  Created by yinda on 2018/6/7.
//  Copyright © 2018年 H.DX. All rights reserved.
//

#import "PickHeaderCell.h"

@implementation PickHeaderCell{
    NSInteger count;
    BOOL canOperation;  // 能够操作加减件数
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        count = 0;
        canOperation = NO;
        [self s_setupCellView];
    }
    return self;
}

- (void)setPlateModel:(PickupModel *)pickupModel {

    self.nameLab.text = pickupModel.partName;
    self.BoxNoLabel.text = pickupModel.partCode;  //版件号
    self.verLab.text = pickupModel.boxCode;  // 箱号
    self.codeLab.text = pickupModel.partSN;  // 序列号
    self.numLab.text = pickupModel.partVersion;   // 数量

    [self layoutSubviews];

}

//- (void)setPlateModel:(PlateModel *)plateModel {
//    
//    self.nameLab.text = plateModel.warehouse;
//    self.BoxNoLabel.text = plateModel.wbs;
//    self.codeLab.text = plateModel.name;
//    self.verLab.text = plateModel.code;
//    self.numLab.text = plateModel.number;
//    
//    [self layoutSubviews];
//    
//}

- (void)s_setupCellView {
    
    self.nameLab = [[RZCustomLable alloc] init];
    self.nameLab.font = CustomFont(13);
    self.nameLab.numberOfLines = 0;
    self.nameLab.textColor = [UtilCheck hexStringToColor:@"6c6c6c" alpha:1];
    self.nameLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.nameLab];
    
    
    self.codeLab = [[RZCustomLable alloc] init];
    self.codeLab.font = CustomFont(13);
    self.codeLab.textColor = [UtilCheck hexStringToColor:@"6c6c6c" alpha:1];
    self.codeLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.codeLab];
    
    self.verLab = [[RZCustomLable alloc] init];
    self.verLab.font = CustomFont(13);
    self.verLab.textColor = [UtilCheck hexStringToColor:@"6c6c6c" alpha:1];
    self.verLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.verLab];
    
    self.numLab = [[RZCustomLable alloc] init];
    self.numLab.font = CustomFont(13);
    self.numLab.textColor = [UtilCheck hexStringToColor:@"6c6c6c" alpha:1];
    self.numLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.numLab];
    
    self.BoxNoLabel = [[RZCustomLable alloc] init];
    self.BoxNoLabel.font = CustomFont(13);
    self.BoxNoLabel.textColor = [UtilCheck hexStringToColor:@"6c6c6c" alpha:1];
    self.BoxNoLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.BoxNoLabel];
    
    self.selectedBt = [[RZCustomButtom alloc]init];
    [self.selectedBt setImage:[UIImage imageNamed:@"未选中"] forState:(UIControlStateNormal)];
    [self.selectedBt setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    [self.selectedBt setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateHighlighted];
    
    [self.selectedBt addTarget:self action:@selector(selectedBt:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectedBt];
    
    self.oprationView = [[UIView alloc]init];
    
    UIButton *subtractionBut = [UIButton buttonWithType:UIButtonTypeCustom];
    subtractionBut.frame = CGRectMake(0, 5, 30, 30);
    subtractionBut.tag = 20180608;
    subtractionBut.hidden  = YES;
    [subtractionBut addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    [subtractionBut setImage:[UIImage imageNamed:@"减"] forState:UIControlStateNormal];

    _numLabel = [[UITextField alloc]initWithFrame:CGRectMake(32, 5, 30, 30)];
    _numLabel.text = @"0";
    _numLabel.hidden = YES;
    _numLabel.textAlignment = NSTextAlignmentCenter;
    _numLabel.font  = [UIFont systemFontOfSize:15];
    [self.oprationView addSubview:_numLabel];
    
    UIButton *additionBut = [UIButton buttonWithType:UIButtonTypeCustom];
    additionBut.frame = CGRectMake(65, 5, 30, 30);
    [additionBut setImage:[UIImage imageNamed:@"加"] forState:UIControlStateNormal];
    additionBut.tag = 20180609;
    [additionBut addTarget:self action:@selector(subAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.oprationView addSubview:additionBut];
    [self.oprationView addSubview:subtractionBut];
    [self.contentView addSubview:self.oprationView];
    
    self.cellHeight = self.nameLab.height;
}
- (void)setAlreadystatus:(NSString *)alreadystatus{
    _alreadystatus = alreadystatus;
    if ([alreadystatus isEqualToString:@"1"]) {
        self.selectedBt.selected = YES;
        UIButton *subbut  = [self viewWithTag:20180608];
        subbut.hidden  = NO;
        _numLabel.hidden = NO;
        canOperation = YES;
    }
}
// 全选的处理
- (void)setIsall:(BOOL)isall{
    if(isall == YES){
    [self.selectedBt setImage:[UIImage imageNamed:@"选中"] forState:(UIControlStateNormal)];
    UIButton *subbut  = [self viewWithTag:20180608];
    subbut.hidden  = NO;
    _numLabel.text =  [NSString stringWithFormat:@"%d",(int)self.numForAlready];
    _numLabel.hidden = NO;
    canOperation = YES;
    }
}
- (void)setNumForAlready:(NSInteger)numForAlready{
    _numForAlready = numForAlready;
    _numLabel.text =  [NSString stringWithFormat:@"%d",(int)self.numForAlready];
}
    
- (void)selectedBt:(UIButton *)bt{
    bt.selected = !bt.selected;
    UIButton *subbut  = [self viewWithTag:20180608];
    subbut.hidden  = !subbut.hidden;
    _numLabel.hidden = !_numLabel.hidden;
    
    if (self.countBlock && !bt.isSelected) {
        self.countBlock([_numLabel.text integerValue],-1);
    }
    else{
       self.countBlock([_numLabel.text integerValue],-2);
    }
    canOperation = bt.isSelected ? YES : NO;
}
- (void)subAction:(UIButton *)but{
    // 首先是否能够操作
    if (canOperation) {
        int num = [_numLabel.text intValue];
        if (but.tag == 20180608) {
            _numLabel.text = [NSString stringWithFormat:@"%d",num - 1];
            // 回调选中的数量
            if (self.countBlock && [_numLabel.text intValue]>=0) {
                self.countBlock([_numLabel.text integerValue],0);
            }
            
            if ([_numLabel.text intValue]<1) {
                _numLabel.text  = @"0";
            }
           
        }
        else{
            
            if (num < [self.numLab.text integerValue]) {
                _numLabel.text = [NSString stringWithFormat:@"%d",num + 1];
                // 回调选中的数量
                if (self.countBlock) {
                    self.countBlock([_numLabel.text integerValue],1);
                }
            }
            else{
                _numLabel.text = [NSString stringWithFormat:@"%d",num];
            }
        }
    }
}

- (void)layoutSubviews
{
    CGRect frame = [self.nameLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH / 5, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
    
    [self.nameLab setFrame:frame];
    self.nameLab.center =CGPointMake(SCREEN_WIDTH / 6.8, self.height/2);
    
    //    self.nameLab.top = 0;
    
    [self.BoxNoLabel setFrame:CGRectMake(SCREEN_WIDTH / 5 + 18 , 0, SCREEN_WIDTH / 6.7, self.height)];
    
    [self.codeLab setFrame:CGRectMake(SCREEN_WIDTH / 5 * 2-15, 0, SCREEN_WIDTH / 6.7, self.height)];
    
    [self.verLab setFrame:CGRectMake(SCREEN_WIDTH / 5.5 * 3-12, 0, SCREEN_WIDTH / 6.7, self.height)];
    
    [self.numLab setFrame:CGRectMake(SCREEN_WIDTH / 5.7 * 4-13, 0, SCREEN_WIDTH / 6.7, self.height)];
    
    [self.selectedBt setFrame:CGRectMake(15 , self.height/5, self.height/2+8, self.height/2+8)];
    
    [self.oprationView setFrame:CGRectMake(SCREEN_WIDTH / 5.7 * 5-30, 0, SCREEN_WIDTH / 6, self.height)];
    
    self.cellHeight = self.verLab.height;
    if (self.cellHeight < 40) {
        self.cellHeight = self.cellHeight + 20;
    }
    
    
}

@end
